package com.it.jzs.service.admin.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.jzs.dao.admin.AdminUserMapper;
import com.it.jzs.pojo.Type;
import com.it.jzs.pojo.User;
import com.it.jzs.service.admin.AdminUserService;
import com.it.jzs.vo.Result;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Resource
    private AdminUserMapper adminUserMapper;

    private final String SLOT = "jzs";   // MD5加盐

    private final String DEFAULT_PASSWORD = "123";

    @Override
    public Result<Object> selectPage(Integer index, Integer size, String keyWords) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!keyWords.equals("")) {    // 如果是搜索查询
            wrapper.like("nickname", keyWords);
        }
        Page<User> page = new Page<>(index, size);
        Page<User> userPage = adminUserMapper.selectPage(page, wrapper);
        return Result.success(userPage, "获取用户分页数据成功！");
    }

    @Override
    public Result insertOne(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        // 表单校验
        if (user.getNickname() == null || user.getLocation() == null || user.getType() == null || user.getUsername() == null)
            return Result.error(null, "表单不能为空");
        wrapper.eq("username", user.getUsername());
        User one = adminUserMapper.selectOne(wrapper);
//        检查是否有相同的用户名
        if (one != null) return Result.error(null, "用户名重复");
        String password = SecureUtil.md5(DEFAULT_PASSWORD + SLOT);
        user.setPassword(password);
        user.setCreateTime(System.currentTimeMillis());
        user.setUpdateTime(System.currentTimeMillis());
        wrapper = new QueryWrapper<User>();
        int insert = adminUserMapper.insert(user);
        if (insert > 0) return Result.success(null, "新增用户成功");
        return Result.error(null, "新增用户失败");

    }

    @Override
    public Result<Object> delete(Integer id) {
        if (id != null) {
//                Type type = typeMapper.selectById(id);
            int i = adminUserMapper.deleteById(id);
            if (i > 0) {
                return Result.success(null, "删除成功！");
            }
        }
        return Result.error(null, "删除失败！");

    }

    @Override
    public Result<Object> update(User user) {

        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        user.setUpdateTime(System.currentTimeMillis());
//        wrapper.select("name").select("type_img").select("update_time");
//        int update = typeMapper.update(type, wrapper);
        // 表单校验
        if (user.getNickname() == null || user.getLocation() == null || user.getRemark() == null)
            return Result.error(null, "修改表单不能为空");
        String password = user.getPassword();
        password = SecureUtil.md5(password + SLOT);
        user.setPassword(password);
        int update = adminUserMapper.updateById(user);
        if (update > 0) {
            return Result.success(null, "修改成功");
        }
        return Result.error(null, "修改失败");
//        int i = typeMapper.updateById(type);
    }

    @Override
    public Result<Object> updateStatus(Integer id, Integer type) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        User user = adminUserMapper.selectOne(wrapper);
        if (user != null) {
            user.setType(type);
            int i = adminUserMapper.updateById(user);
            if (i > 0) {
                return Result.success(null, "修改用户状态成功");
            }
            return Result.error(null, "修改用户状态失败");
        }
        return Result.error(null, "没有此用户");
    }

    @Override
    public Result<Object> deleteBatch(List<Integer> ids) {
        if (ids.size() > 0) {
            int i = adminUserMapper.deleteBatchIds(ids);
            if (i > 0) {
                return Result.success(null, "批量删除成功");
            }
        }


        return Result.error(null, "删除失败");
    }
}
